package com.guanyu.u2cache.api;

import com.guanyu.u2cache.cache.CacheOperate;
import com.guanyu.u2cache.constant.CacheConstant;
import com.guanyu.u2cache.enums.CacheType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouyunjian
 * @date 2023/12/08 11:39
 * u2Cache手动调用api接口信息
 * 支持对外接口显性调用
 **/
@Service
public class U2CacheLevel2Api<K, V> {

    @Autowired
    private CacheOperate cacheOperate;

    /**
     * 功能描述: 根据Key获取list列表
     *
     * @param key
     * @return java.lang.Object
     * @author zgy
     * @date 2021/6/2
     */
    public List<V> findListByKey(String key) {
        return (List<V>) cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, 0, TimeUnit.SECONDS);
    }

    /**
     * 功能描述: 将key,List塞入到本地缓存中
     *
     * @param key
     * @param expireTime
     * @param timeUnit
     */
    public List<K> putListByKeyExpire(String key, List<K> inputList, long expireTime, TimeUnit timeUnit) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, inputList, null, expireTime, timeUnit);
        return inputList;
    }


    /**
     * 功能描述: 将key,Object塞入到本地缓存中
     *
     * @param key
     * @return java.lang.Object
     * @author zgy
     * @date 2021/6/2
     */
    public Object putObjectByKey(String key, Object result) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, result, null, 0, TimeUnit.SECONDS);
        return result;
    }

    /**
     * 功能描述: 将key,Object塞入到本地缓存中
     *
     * @param key
     * @return java.lang.Object
     * @author zgy
     * @date 2021/6/2
     */
    public Object putObjectByKeyExpire(String key, Object result) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, result, null, 0, TimeUnit.SECONDS);
        return result;
    }

    /**
     * 功能描述: 根据key删除对应缓存结果
     *
     * @param key
     * @return java.lang.Object
     * @author zgy
     * @date 2021/6/2
     */
    public void deleteInfoByKey(String key) {
        cacheOperate.clear(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE);
    }

    /**
     * 获取全量redis列表
     *
     * @param key          redis key
     * @param valueListMap value
     * @param time         缓存时间
     * @param timeUnit     缓存时间单位
     * @param <O>          泛型
     * @return 列表
     */
    public <O> Map<String, List<O>> putAllList(String key, Map<String, List<O>> valueListMap, long time, TimeUnit timeUnit) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, valueListMap, null, time, timeUnit);
        return valueListMap;
    }

    /**
     * 获取全量redis列表
     *
     * @param key      redis key
     * @param hashKey  key
     * @param value    value
     * @param time     缓存时间
     * @param timeUnit 缓存时间单位
     * @param <O>      泛型
     * @return 列表
     */
    public <O> O put(String key, String hashKey, O value, long time, TimeUnit timeUnit) {
        Map<Object, Object> cache = (Map<Object, Object>) cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, time, timeUnit);
        cache.put(hashKey, value);
        return value;
    }

    /**
     * 获取全量列表
     *
     * @param key     key
     * @param hashKey hashKey
     * @return 全量列表
     */
    public Object findMapByKey(String key, String hashKey) {
        Map<Object, Object> cache = (Map<Object, Object>) cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, 0, TimeUnit.SECONDS);
        return cache.get(hashKey);
    }

    /**
     * 查找value对象
     *
     * @param key key
     * @return 全量列表
     */
    public Object findObjectByKey(String key) {
        return cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, 0, TimeUnit.SECONDS);
    }

    /**
     * 塞入value对象
     *
     * @param key   redis key
     * @param value value
     * @param <O>   泛型
     * @return 列表
     */
    public <O> O put(String key, Class returnType, O value) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, value, returnType, 0, TimeUnit.SECONDS);
        return value;
    }

    /**
     * 塞入value对象,设置过期时间
     *
     * @param key   redis key
     * @param value value
     * @param <O>   泛型
     * @return 列表
     */
    public <O> O put(String key, Class returnType, O value, long time, TimeUnit timeUnit) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, value, returnType, time, timeUnit);
        return value;
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param time
     * @param timeUnit
     */
    public void expire(String key, long time, TimeUnit timeUnit) {
        Object value = cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, 0, TimeUnit.SECONDS);
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, value, null, time, timeUnit);
    }
    
    /**
     * 获取全量列表
     *
     * @param key key
     * @return 全量列表
     */
    public List<Object> findMapByKey(String key) {
        Map<Object, Object> cache = (Map<Object, Object>) cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, 0, TimeUnit.SECONDS);
        return new ArrayList<>(cache.values());
    }

    /**
     * 获取全量列表
     *
     * @param key key
     * @return 全量列表
     */
    public Map<String, List<Object>> findAllMapByKey(String key) {
        return (Map<String, List<Object>>) cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE, 0, TimeUnit.SECONDS);
    }

    /**
     * 功能描述: 根据key删除对应缓存结果
     *
     * @param key key
     * @return java.lang.Object
     */

    public Boolean deleteByKey(String key) {
        cacheOperate.clear(new String[]{CacheConstant.operateCacheName}, key, CacheType.LEVEL2CACHE);
        return true;
    }
}
